package com.gerenboke.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gerenboke.constans.SystemConstants;
import com.gerenboke.domain.ResponseResult;
import com.gerenboke.domain.dto.RoleDto;
import com.gerenboke.domain.dto.RoleSaveDto;
import com.gerenboke.domain.dto.RoleUpdateDto;
import com.gerenboke.domain.entity.Role;
import com.gerenboke.domain.vo.PageVo;
import com.gerenboke.domain.vo.RoleListVo;
import com.gerenboke.domain.vo.RoleVo;
import com.gerenboke.enums.AppHttpCodeEnum;
import com.gerenboke.mapper.MenuMapper;
import com.gerenboke.mapper.RoleMapper;
import com.gerenboke.service.RoleService;
import com.gerenboke.utils.BeanCopyUtils;
import com.gerenboke.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 角色信息表(SysRole)表服务实现类
 *
 * @author makejava
 * @since 2022-11-16 23:25:41
 */
@Service("RoleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Override
    public List<String> selectRoleKeyByUserId(Long id) {
        //判断是否是管理员 如果是返回集合中只需要有admin
        if(SecurityUtils.isAdmin()){
            List<String> roleKeys = new ArrayList<>();
            roleKeys.add("admin");
            return roleKeys;
        }
        //否则查询用户所具有的角色信息
        return getBaseMapper().selectRoleKeyByUserId(id);
    }

    @Override
    public ResponseResult RoleList(Long pageNum, Long pageSize, String roleName, String status) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Objects.nonNull(roleName),Role::getRoleName,roleName);
        queryWrapper.eq(Objects.nonNull(status),Role::getStatus,status);
        queryWrapper.orderByAsc(Role::getRoleSort);
        Page<Role> page = new Page<>(pageNum,pageSize);
        page(page,queryWrapper);
        List<RoleListVo> roleListVos = BeanCopyUtils.copyBeanList(page.getRecords(), RoleListVo.class);
        return ResponseResult.okResult(new PageVo(roleListVos,page.getTotal()));
    }

    @Override
    public ResponseResult updateRole(RoleDto roleDto) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getId,roleDto.getRoleId());
        queryWrapper.eq(Role::getStatus,roleDto.getStatus());
        boolean update = update(queryWrapper);
        return ResponseResult.okResult();
    }

    @Override
    @Transactional
    public ResponseResult insertRole(RoleSaveDto roleSaveDto) {
        Role role = BeanCopyUtils.copyBean(roleSaveDto, Role.class);
        boolean save = save(role);
        roleSaveDto.getMenuIds().stream().map(menuId -> getBaseMapper().insertRoleAndMenu(role.getId(),Long.valueOf(menuId))).count();
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult selectRole(Long id) {
        Role role = getById(id);
        RoleVo roleVo = BeanCopyUtils.copyBean(role, RoleVo.class);
        return ResponseResult.okResult(roleVo);
    }

    @Override
    @Transactional
    public ResponseResult updateRoleMenu(RoleUpdateDto roleUpdateDto) {
        Role role = BeanCopyUtils.copyBean(roleUpdateDto, Role.class);
        int i = getBaseMapper().updateById(role);
        getBaseMapper().deleteRoleAndMenu(roleUpdateDto.getId());
        roleUpdateDto.getMenuIds().stream().map(menuId -> getBaseMapper().insertRoleAndMenu(role.getId(), Long.valueOf(menuId))).count();
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteRole(String id) {
        getBaseMapper().deleteById(id);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult listAllRole() {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        List<Role> list = list(queryWrapper);
        return ResponseResult.okResult(list);
    }
}

